package com.ruoyi.odorservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.odorservice.entity.Craft;
import com.ruoyi.odorservice.entity.CraftOdor;
import com.ruoyi.odorservice.entity.Industry;
import com.ruoyi.odorservice.entity.IndustryCraft;
import com.ruoyi.odorservice.mapper.CraftMapper;
import com.ruoyi.odorservice.mapper.CraftOdorMapper;
import com.ruoyi.odorservice.mapper.IndustryCraftMapper;
import com.ruoyi.odorservice.mapper.IndustryMapper;
import com.ruoyi.odorservice.model.bean.CraftIndustryAssociationBean;
import com.ruoyi.odorservice.model.bean.CraftInfoListBean;
import com.ruoyi.odorservice.model.bean.CraftOdorListBean;
import com.ruoyi.odorservice.model.vo.CraftListVo;
import com.ruoyi.odorservice.model.vo.CraftOdorListVo;
import com.ruoyi.odorservice.service.CraftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CraftServiceImpl extends ServiceImpl<CraftMapper, Craft> implements CraftService {

    @Autowired
    private IndustryMapper industryMapper;

    @Autowired
    private IndustryCraftMapper industryCraftMapper;

    @Autowired
    private CraftOdorMapper craftOdorMapper;

    @Override
    public Result<CraftListVo> craftList(CraftInfoListBean craftInfoListBean) {
        Integer page = craftInfoListBean.getPage();
        Integer limit = craftInfoListBean.getLimit();
        String craftName = craftInfoListBean.getCraftName();
        Integer industryId = craftInfoListBean.getIndustryId();
        List<Craft> craftList = this.baseMapper.selectCraftPage((page - 1) * limit, limit, craftName, industryId);
        Integer total = this.baseMapper.selectCraftPageCount(craftName, industryId);
        CraftListVo craftListVo = new CraftListVo(total, craftList);
        return Result.ok(craftListVo);
    }

    @Override
    @Transactional
    public Result<Boolean> addCraft(CraftIndustryAssociationBean craftIndustryAssociationBean) {
        if(hasSameCraftName(craftIndustryAssociationBean.getCraftName(), craftIndustryAssociationBean.getIndustryId(), craftIndustryAssociationBean.getId())) return Result.fail(false,"存在相同名称");
        if(!checkIndustryExist(craftIndustryAssociationBean.getIndustryId())) return Result.fail(false, "行业不存在");
        Craft craft = new Craft();
        IndustryCraft industryCraft = new IndustryCraft();

        craft.setCraftName(craftIndustryAssociationBean.getCraftName());
        craft.setRemark(craftIndustryAssociationBean.getRemark());

        this.baseMapper.insert(craft);
        industryCraft.setCraftId(craft.getId());
        industryCraft.setIndustryId(craftIndustryAssociationBean.getIndustryId());
        industryCraftMapper.insert(industryCraft);

        return Result.ok(true);
    }

    @Override
    @Transactional
    public Result<Boolean> updateCraft(CraftIndustryAssociationBean craftIndustryAssociationBean) {
        if(hasSameCraftName(craftIndustryAssociationBean.getCraftName(), craftIndustryAssociationBean.getIndustryId(),craftIndustryAssociationBean.getId())) return Result.fail(false,"存在相同名称");
        if(craftIndustryAssociationBean.getIndustryId() != null && !checkIndustryExist(craftIndustryAssociationBean.getIndustryId())) return Result.fail(false, "行业不存在");
        if(craftIndustryAssociationBean.getIndustryId() != null) {
            LambdaUpdateWrapper<IndustryCraft> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(IndustryCraft::getIndustryId, craftIndustryAssociationBean.getIndustryId());
            updateWrapper.eq(IndustryCraft::getCraftId, craftIndustryAssociationBean.getId());
            industryCraftMapper.update(new IndustryCraft(craftIndustryAssociationBean.getIndustryId(), craftIndustryAssociationBean.getId()), updateWrapper);
        }
        Craft craft = new Craft();

        craft.setId(craftIndustryAssociationBean.getId());
        craft.setCraftName(craftIndustryAssociationBean.getCraftName());
        craft.setRemark(craftIndustryAssociationBean.getRemark());

        this.baseMapper.updateById(craft);

        return Result.ok(true);
    }

    @Override
    @Transactional
    public Result<Boolean> addOdor(CraftOdor craftOdor) {
        craftOdorMapper.insert(craftOdor);
        return Result.ok(true);
    }

    @Override
    public Result<CraftOdorListVo> craftOdorList(CraftOdorListBean craftOdorListBean) {
        Integer page = craftOdorListBean.getPage();
        Integer limit = craftOdorListBean.getLimit();
        String industryName = craftOdorListBean.getIndustryName();
        String craftName = craftOdorListBean.getCraftName();
        String assessor = craftOdorListBean.getAssessor();
        String odorName = craftOdorListBean.getOdorName();

        List<CraftOdor> craftOdorList = craftOdorMapper.selectCraftOdorList((page - 1) * limit, limit, industryName, craftName, assessor, odorName);
        Integer total = craftOdorMapper.selectCraftOdorListTotal(industryName, craftName, odorName, assessor);

        CraftOdorListVo craftOdorListVo = new CraftOdorListVo(total, craftOdorList);

        return Result.ok(craftOdorListVo);

    }

    private Boolean hasSameCraftName(String craftName, Integer industryId, Integer craftId) {
        Integer id = this.baseMapper.selectSameCraftName(craftName, industryId);
        return id != null && !id.equals(craftId);
    }
    private Boolean checkIndustryExist(Integer industryId) {
        Industry industry = industryMapper.selectById(industryId);
        return industry != null;
    }
}
